package com.jiinfo.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.util.UUIDUtils;

public class Dic extends Model<Dic>{

	private static final long serialVersionUID = -6038491336537962898L;
	public static final Dic dao = new Dic();
	
	public Page<Record> pagequery(Integer pageNumber,Integer pageSize,Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_dic_node t where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		
		sqlExceptSelect.append(" order by t.updateTime desc");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		return Db.paginate(pageNumber,pageSize,"select t.*",sqlExceptSelect.toString(),list.toArray());
	}
	
	public Dic add(Dic vo,Record user){
		Dic dic=new Dic();
		dic.set("id", UUIDUtils.getUUID());
		dic.set("createTime", new Date());
		dic.set("creator", user.getStr("id"));
		dic.set("updateTime", new Date());
		dic.set("updator", user.getStr("id"));
		dic.set("removed", "0");
		
		dic.set("name", vo.getStr("name"));
		dic.set("code", vo.getStr("code"));
		dic.set("desp", vo.getStr("desp"));
		dic.set("seq",vo.getInt("seq"));
		dic.set("parentId",vo.getStr("parentId"));
		
		dic.save();
		return dic;
	}
	
	public Dic edit(Dic vo,Record user){
		Dic dic=Dic.dao.findById(vo.getStr("id"));
		dic.set("updateTime", new Date());
		dic.set("updator", user.getStr("id"));
		
		dic.set("name", vo.getStr("name"));
		dic.set("code", vo.getStr("code"));
		dic.set("desp", vo.getStr("desp"));
		dic.set("seq",vo.getInt("seq"));
		dic.set("parentId",vo.getStr("parentId"));
		
		dic.update();
		return dic;
	}
	
	public Dic remove(String id){
		Dic dic=Dic.dao.findById(id);
		dic.set("removed", "1");
		dic.update();
		return dic;
	}
	
	public List<Record> getTopdics(){
		String sql="select r.* from t_dic_node r "
				+ " where r.removed='0' "
				+ " and (r.parentId is null or r.parentId='' )  "
				+ " order by r.seq  ";
		return Db.find(sql);
	}
	
	public List<Record> getAlldicsByParentId(String parentId){
		String sql="select r.* from t_dic_node r "
				+ " where r.removed='0' "
				+ " and r.parentId=? "
				+ " order by r.seq  ";
		return Db.find(sql,parentId);
	}
	
	public Record getFullTreedic(String childrenName,int parentLevel,Record record){
		List<Record> subList=getAlldicsByParentId(record.getStr("id"));
		List<Record> list=new ArrayList<Record>();
		int subLevel=parentLevel+1;
		if(subList.size()>0){
			for (Record record2 : subList) {
				record.set("treeLevel", subLevel);
				list.add(getFullTreedic(childrenName,subLevel,record2));
			}
		}
		record.set(childrenName, list);
		record.set("treeLevel", parentLevel);
		return record;
	}
	
	public List<Record> getTreedics(String childrenName){
		List<Record> list=getTopdics();
		int parentLevel=1;
		for (Record record : list) {
			getFullTreedic(childrenName,parentLevel,record);
		}
		return list;
	}
}
